﻿using System;
using System.Windows.Forms;
using MediaController.Client.Controls.Dialogs;
using MediaController.Client.DAL;
using MediaController.Utils;

namespace MediaController
{
    public partial class MainForm : Form
    {
        //private readonly ConsoleRedirector _consoleRedirector;

        public MainForm()
        {
            InitializeComponent();
            //_consoleRedirector = new ConsoleRedirector(main1.Logger);
            //Console.SetOut(_consoleRedirector);
        }

        private void ShowForm()
        {
            Show();
            WindowState = FormWindowState.Normal;
            Activate();
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataAccess.Instance.SaveData();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void StopToolStripButtonClick(object sender, EventArgs e)
        {
            Watcher.Watcher.Intance.Stop();
        }

        private void StartToolStripButtonClick(object sender, EventArgs e)
        {
            if (!Watcher.Watcher.Intance.Started)
            {
                Watcher.Watcher.Intance.StartAsync();
            }
            else
            {
                Watcher.Watcher.Intance.Resume();
            }

            _toolStripButtonStart.Enabled = false;
            _toolStripButtonPause.Enabled = !_toolStripButtonStart.Enabled;
            //_toolStripButtonStopAll.Enabled = !_toolStripButtonStart.Enabled;
        }

        private void OptionsToolStripButtonClick(object sender, EventArgs e)
        {
            var propertiesForm = new PropertiesForm();
            if (propertiesForm.ShowDialog() == DialogResult.OK)
            {
                DataAccess.Instance.SaveData();
            }
        }

        private void ResetToolStripButtonClick(object sender, EventArgs e)
        {
            var dialogResult =
                MessageBox.Show("You are about to reset all application setting.\nhow would you like to proceed?",
                                "Reset data", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning,
                                MessageBoxDefaultButton.Button2);
            if (dialogResult == DialogResult.Yes)
            {
                DataAccess.Instance.Reset();
            }
        }

        private void ExtractedFilesToolStripMenuItemClick(object sender, EventArgs e)
        {
            DataAccess.Instance.MediaControllerDataSet.Files.Clear();
        }

        private void WatchFoldersToolStripMenuItemClick(object sender, EventArgs e)
        {
            DataAccess.Instance.MediaControllerDataSet.Listeners.Clear();
        }

        private void ToolStripButtonPauseClick(object sender, EventArgs e)
        {
            Watcher.Watcher.Intance.Pause();
            _toolStripButtonPause.Enabled = false;
            _toolStripButtonStart.Enabled = !_toolStripButtonPause.Enabled;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();
        }

        private void NotifyIconMouseDoubleClick(object sender, MouseEventArgs e)
        {
            ShowForm();
        }
     
        private void ShowToolStripMenuItemClick(object sender, EventArgs e)
        {
            ShowForm();
        }
     
        private void ExitToolStripMenuItemClick(object sender, EventArgs e)
        {
            Close();
        }

        private void ClearLogToolStripMenuItemClick(object sender, EventArgs e)
        {
            DataAccess.Instance.MediaControllerDataSet.Logger.Clear();
        }
    }
}
